﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using System.Text;
using System.Xml;
using Gift.WebApp;
using Gift.Model;
using Gift.DAL;

namespace WebApp.admin.news
{
    public partial class articleList : BaseAdmin
    {
        // Fields
        protected string atId = "00000000-0000-0000-0000-000000000000";

        protected string kw = "";
        protected string order = "id";
        protected int ot = 1;
        protected int pageCount = 0;
        protected int pageIndex = 1;
        protected int pageSize = 20;
        protected PageList pl = new PageList();
        protected int t = 0;
        protected int tid = 0;
        protected int total = 0;

        // Methods
        private void ArticleBind()
        {
            try
            {
                if (base.Request.QueryString["p"] != null)
                {
                    this.pageIndex = int.Parse(base.Request.QueryString["p"]);
                }
                if (base.Request.QueryString["ps"] != null)
                {
                    this.pageSize = Convert.ToInt32(base.Request.QueryString["ps"]);
                }
                if (base.Request.QueryString["or"] != null)
                {
                    this.order = base.Request.QueryString["or"];
                }
                if (base.Request.QueryString["ot"] != null)
                {
                    this.ot = Convert.ToInt32(base.Request.QueryString["ot"]);
                }
                if (base.Request.QueryString["rid"] != null)
                {
                    this.tid = Convert.ToInt32(base.Request.QueryString["tid"]);
                }
                this.pl.TblName = "view_article";
                this.pl.StrGetFields = "id,articleTilte,typeIdentity,typeName,rootId,author,clickCount,convert(nvarchar(10),createTime,121) as createTime,isShow";
                this.pl.FldName = this.order;
                this.pl.PageSize = this.pageSize;
                this.pl.PageIndex = this.pageIndex;
                this.pl.DoCount = 1;
                this.pl.OrderType = this.ot;
                if ((base.Request.QueryString["t"] != null) && (base.Request.QueryString["kw"] != null))
                {
                    this.kw = base.Request.QueryString["kw"];
                    if (this.tid == 0)
                    {
                        this.pl.StrWhere = base.Request.QueryString["t"] + " like '%" + this.kw + "%'";
                    }
                    else
                    {
                        this.pl.StrWhere = string.Concat(new object[] { base.Request.QueryString["t"], " like '%", this.kw, "%' and typeIdentity=", this.tid });
                    }
                }
            
                else
                {
                    this.pl.StrWhere = string.Concat(new object[] { "typeIdentity=", this.tid });
                }
                if (base.Request.QueryString["atId"] != null)
                {
                    this.atId = base.Request.QueryString["atId"];
                }
                string strWhere = this.pl.StrWhere;
                this.pl.StrWhere = strWhere + " and (parentIdStr like '%" + this.atId + "%' or atId='" + this.atId + "')";
                this.total = Convert.ToInt32(BaseFactory.thePageList.GetPageList(this.pl).Rows[0]["Total"]);
                this.pl.DoCount = 0;
                DataTable pageList = BaseFactory.thePageList.GetPageList(this.pl);
                new PageListNo().DelPageSize(pageList, this.total);
                if (pageList.Rows.Count == 0)
                {
                    this.btnDelete.Visible = false;
                }
                this.rptArticle.DataSource = pageList.DefaultView;
                this.rptArticle.DataBind();
            }
            catch
            {
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string str = base.Request.Form["Item"];
            if (str.Contains(","))
            {
                Array array = str.Split(new char[] { ',' });
                foreach (string str2 in array)
                {
                    BaseFactory.theArticle.Delete(Convert.ToInt32(str2));
                }
            }
            else
            {
                BaseFactory.theArticle.Delete(Convert.ToInt32(str));
            }
            base.Response.Redirect(HttpContext.Current.Request.Url.PathAndQuery);
        }

        protected bool DisplayDeleteArticle(object id)
        {
            bool flag = true;
            try
            {
                if ((((Convert.ToInt32(id) >= 1) && (Convert.ToInt32(id) <= 15)) || (Convert.ToInt32(id) == 24)) || (Convert.ToInt32(id) == 25))
                {
                    return false;
                }
                flag = true;
            }
            catch
            {
            }
            return flag;
        }

        protected string GetCheckBox(object id)
        {
            string str = "";
            try
            {
                if ((((Convert.ToInt32(id) >= 1) && (Convert.ToInt32(id) <= 15)) || (Convert.ToInt32(id) == 24)) || (Convert.ToInt32(id) == 25))
                {
                    return "";
                }
                str = "<input id='cb" + id.ToString() + "' name=\"Item\" type=\"checkbox\" onclick=\"ShowDelete();\" value='id' />";
            }
            catch
            {
            }
            return str;
        }

        protected override object LoadPageStateFromPersistenceMedium()
        {
            byte[] inArray = ViewStateCompression.DeCompress(Convert.FromBase64String(base.Request.Form.Get("__MSPVSTATE")));
            LosFormatter formatter = new LosFormatter();
            return formatter.Deserialize(Convert.ToBase64String(inArray));
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!base.IsPostBack)
            {
                base.ValidateLoginRule(Gift.Model.ENUM.ERule.ArticleManage);
                if (base.Request.QueryString["t"] != null)
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "sa", "<script type=\"text/javascript\">selectIsExitItem(\"selectArticle\",\"" + base.Request.QueryString["t"] + "\");</script>");
                }
                if (base.Request.QueryString["ps"] != null)
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "sPs", "<script type=\"text/javascript\">selectIsExitItem(\"selectPageSize\",\"" + base.Request.QueryString["ps"] + "\");</script>");
                }
                this.btnDelete.Attributes.Add("disabled", "disabled");
                this.ArticleBind();
            }
        }

        protected void rptArticle_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string pathAndQuery = HttpContext.Current.Request.Url.PathAndQuery;
            int id = Convert.ToInt32(e.CommandArgument);
            if (e.CommandName == "del")
            {
                if (BaseFactory.theArticle.Delete(id))
                {
                    base.Response.Redirect(pathAndQuery);
                }
                else
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "sd", "<script language=\"javascript\" defer>alert('删除失败！');location.href='" + pathAndQuery + "';</script>");
                }
            }
        }

        protected override void SavePageStateToPersistenceMedium(object pViewState)
        {
            LosFormatter formatter = new LosFormatter();
            StringWriter output = new StringWriter();
            formatter.Serialize(output, pViewState);
            string hiddenFieldInitialValue = Convert.ToBase64String(ViewStateCompression.Compress(Convert.FromBase64String(output.ToString())));
            base.ClientScript.RegisterHiddenField("__MSPVSTATE", hiddenFieldInitialValue);
        }
    }
}
